﻿using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Stu_sys.Entity;
using Stu_sys.Models;
using Stu_sys.utils;
using System.Text.Json;

namespace Stu_sys.Controllers
{
    public class UserController : Controller
    {
        private readonly MysqlContext _mysqlContext;
        public UserController(MysqlContext mysqlContext) 
        {
            _mysqlContext=mysqlContext;
        }
        public IActionResult UserList()
        {
            return View();
        }
        public IActionResult UserManager()
        {
            return View();        
        }

        [HttpGet]
        public IActionResult GetUserList()
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(UserViewModel));
            var userList = _mysqlContext.Users.Include(u => u.Role).Include(s=>s.Subject)
                .Include(d=>d.Department).Include(o=>o.OfficeRoom).ToList();
            List<UserViewModel> userViewModels = new List<UserViewModel>();
            foreach (var user in userList)
            {
                var u=new UserViewModel() { 
                    UserId=user.UserId,
                    UserName=user.UserName,
                    Password    =user.Password,
                    RoleId=user.RoleId,
                    RoleName=user.Role?.RoleName,
                    RealName=user.RealName,
                    Phone=user.Phone,
                    QQ=user.QQ,
                    SubjectId=user.SubjectId,
                    SubjectName=user.Subject?.SubjectName,
                    DepartmentId=user.DepartmentId,
                    DepartmentName=user.Department?.DepName,
                    OfficeRoomId=user.OfficeRoom.OfficeRoomId,
                    OfficRoomName=user.OfficeRoom.OfficeRoomName
                };
                userViewModels.Add(u);
            }

            var userStr = JsonSerializer.Serialize(userViewModels);
            var data = new
            {
                attr = attrStr,
                row = userStr
            };
            return Json((data));
        }

        [HttpPost]
        public IActionResult AddUser(User user)
        {
            if (user.RoleId<=0||user.SubjectId<=0||user.DepartmentId<=0)
            {
                return Json(0);
            }
            if (user == null)
            { 
                return Json(-1);
            }
            _mysqlContext.Users.Add(user);
            var res=_mysqlContext.SaveChanges();
            return Json(res);
        }

        [HttpPost]
        public IActionResult EditUser(User user)
        {
            if (user.UserName == "" || user.Password == "")
            {
                return Json(-1);
            }

            var u = _mysqlContext.Users.Where(u => u.UserId == user.UserId).FirstOrDefault();
            u.UserName=user.UserName;
            u.Password=user.Password;
            u.RoleId = user.RoleId;
            u.SubjectId =user.SubjectId;
            u.Phone =user.Phone;
            u.RealName =user.RealName;
            u.QQ =user.QQ;
            u.DepartmentId = user.DepartmentId;
            u.OfficeRoomId=user.OfficeRoomId;

            _mysqlContext.Users.Update(u);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }


        [HttpPost]
        public IActionResult DelUser(User user)
        {
            if (user.UserId<=0||user == null)
            {
                return Json(-1);
            }
            var d = _mysqlContext.Users.Single(u => u.UserId == user.UserId);
            _mysqlContext.Remove(d);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }
    }
}
